//Copyright (c) 2007 Andrew Badera

//Permission is hereby granted, free of charge, to any person
//obtaining a copy of this software and associated documentation
//files (the "Software"), to deal in the Software without
//restriction, including without limitation the rights to use,
//copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the
//Software is furnished to do so, subject to the following
//conditions:

//The above copyright notice and this permission notice shall be
//included in all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//OTHER DEALINGS IN THE SOFTWARE.


using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

using JsonFx.Json;

using BadWeb;

namespace TwitterNET
{
    public class Status
    {
        public static string STATUS_UPDATE = "http://twitter.com/statuses/update.json";

        public static string UpdateStatus(string status)
        {

            //after checking for URL and trying to get tiny url
            //check final length

            int statusLength = status.Trim().Length;

            if (statusLength > 140)
            {
                string overlength = ((int)(statusLength-140)).ToString();

                throw new ArgumentException(string.Format("Status message is {0} characters too long.", overlength));
            }

            HttpVar varStatus = new HttpVar("status", status);
            HttpVar varSource = new HttpVar("source", "Flipowitter");
            List<HttpVar> vars = new List<HttpVar>();
            vars.Add(varStatus);
            vars.Add(varSource);

            HttpHeader hdrClient = new HttpHeader("X-Twitter-Client", "Flipowitter");
            HttpHeader hdrVersion = new HttpHeader("X-Twitter-Version", "0.1");
            HttpHeader hdrURL = new HttpHeader("X-Twitter-URL", "http://andrew.badera.us/");

            List<HttpHeader> headers = new List<HttpHeader>();
            headers.Add(hdrClient);
            headers.Add(hdrVersion);
            headers.Add(hdrURL);

            string response = Post.DoPost(STATUS_UPDATE, vars, headers, "application/x-www-form-urlencoded", User.USERNAME, User.PASSWORD);

            return response;
        }
    }
}
